(function () {
    function Person(firstName, lastName, age) {
        var self = this;
        this._fname = firstName;
        this._lname = lastName;
        this._age = age;

        Person.prototype.toString = function () {
            return self._fname + ' ' + self._lname + '(age ' + self._age + ')';
        };
    }

    var persons = [];
    persons.push(new Person("Scott", "Guthrie", 38));
    persons.push(new Person("Scott", "Johns", 36));
    persons.push(new Person("Scott", "Hanselman", 39));
    persons.push(new Person("Jesse", "Liberty", 57));
    persons.push(new Person("Jon", "Skeet", 38));

    function groupByFirstName(array) {
        var groupedResult =
            _.chain(array)
                .groupBy('_fname')
                .value();

        return groupedResult;
    }

    function groupByLastName(array) {
        var groupedResult =
            _.chain(array)
                .groupBy('_lname')
                .value();

        return groupedResult;
    }

    function groupByAge(array) {
        var groupedResult =
            _.chain(array)
                .groupBy('_age')
                .value();

        return groupedResult;
    }

    function group(array, property) {
        var result = {};
        switch (property) {
            case 'firstname': result = groupByFirstName(array); break;
            case 'lastname': result = groupByLastName(array); break;
            case 'age': result = groupByAge(array); break;
        }

        return result;
    }

    console.log(group(persons, 'firstname'));
    console.log(group(persons, 'lastname'));
    console.log(group(persons, 'age'));

    // <3 UnderscoreJS
}());